#coding=utf-8


import sys
import os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from datetime import datetime

from pytw.core.main import MainLoop
from pytw.web.client import HTTPSClient, HTTPClient, SyncHTTPClient
from pytw.core.tasklet import Tasklet

from pytw.web.server import (start_http_server, Route, 
        resource, LeafResource)
from pytw.storage.db import model
from pytw.storage.db.pool import ConnectionPool
from pytw.tools import log


def do_http():
    try:
        uri = 'https://www.hao123.com/'
        body = '13123'
        params = {'abc':123}
        headers = {'content-type':'text/html'}
        client = SyncHTTPClient(uri, 'GET', body, params, headers, timeout = 10, check_cacert = False)
        d = client.request()
        response = client.get_response(d)
        print response.content[:32]
    except Exception as e:
        print 99999, e

@Route.route('/admin/login/')
class TestResource(LeafResource):

    def render_GET(self, request):
        return "TestResource"

@Route.route('/admin/manager')
class ManagerResource(LeafResource):

    def render_GET(self, request):
        return 'ManagerResource'


dbpool = ConnectionPool(
    'pymysql',
    host = '192.168.2.101',
    port = 3306,
    user = 'chinazong',
    passwd = '123456',
    db = 'bas',
    charset = 'utf8'
    )

options = {
    'host': '192.168.2.101',
    'port': 3306,
    'user': 'chinazong',
    'passwd': '123456',
    'charset':'utf8',
    'db':'Bas',
    'cp_min':1,
    'cp_max':1,
    }
dbpool = ConnectionPool('pymysql', **options)

class GlobalConstTable(model.BaseModel):

    __defaults__ = {
        'fdd_time':datetime.now
    }

    __pool__ = dbpool
    __table__ = 'tb_global_const'

def table_test():
    #GlobalConstTable.insert(fdc_key = 'test22222', fdc_value = 'xxxxxx').execute()
    result = GlobalConstTable.select('fdc_key', 'fdc_value', 'fdd_time').filter(fdc_key = 'aa').execute()
    log.info(result)

if __name__ == '__main__':
    Tasklet.create(do_http)
    start_http_server(8888)
   
    log.info("start 8888888 ")
    #Tasklet.create(table_test) 
    MainLoop.run()
